/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* modifiedList(vector<int>& nums, ListNode* head) {
        ListNode dummy(0, head);
        ListNode *newhead=&dummy;
        set<int> flag(nums.begin(), nums.end());

        for(ListNode *pre=newhead, *u=head; u;)
        {
            if(flag.contains(u->val)) pre->next=u->next, u=u->next;
            else pre=u, u=u->next;
        }
        return newhead->next;
    }
};